package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.entity.Tick;
import com.markerhub.service.TickService;
import com.markerhub.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 */
@RestController
//@RequestMapping("/tick")
public class TickController {

    @Autowired
    TickService tickService;

    @GetMapping("/ticks")
    public Result tickall(@RequestParam(defaultValue = "1") Integer currentpage){
        Page page = new Page(currentpage,5);//↓全条件查询？
        IPage pageData= tickService.page(page,new QueryWrapper<Tick>().orderByDesc("created"));
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }






    @GetMapping("/newticks")//新的列表查询
    public Result newtickall(@RequestParam(defaultValue = "1") Integer currentpage){
        Page page = new Page(currentpage,5);//↓全条件查询？
        IPage pageData= tickService.getTickMsgToName(page);
        System.out.println(currentpage);
        System.out.println(pageData.getSize());
        return  Result.succ(pageData);
    }


    @GetMapping("/ticksByName/{id}")//根据标题条件查询
    public Result tickByname(@RequestParam(defaultValue = "1")  Integer currentpage , @PathVariable(name="id") Long id){
        System.out.println("id:"+id);
        Page page = new Page(currentpage,5);//
        Tick tick = tickService.getById(id);

        String title=tick.getTitle();//查找后的名字
        System.out.println("++++++++++++++++++++++++++++++++++++++++++++"+title);
        IPage pageData= tickService.page(page,new QueryWrapper<Tick>().like("title",title));
        return  Result.succ(pageData);
    }


    @GetMapping("/ticksFindName")//根据标题条件查询
    public Result tickFindName(@RequestParam(defaultValue = "1")  Integer currentpage , @RequestParam(required = false,defaultValue = "") String title){
        System.out.println("title:"+title);
        Page page = new Page(currentpage,10);//
        IPage pageData= tickService.findTickMsg(page,title);
        return  Result.succ(pageData);
    }





    @GetMapping("/ticksByAdd")//根据地址条件查询
    public Result tickByadd(@RequestParam(defaultValue = "1")  Integer currentpage , @RequestParam(required = false,defaultValue = "东莞") String address){
        System.out.println("title:"+address);
        Page page = new Page(currentpage,5);//
        IPage pageData= tickService.page(page,new QueryWrapper<Tick>().like("description",address));
        return  Result.succ(pageData);
    }


    @GetMapping("/ticks/{id}")//查询单一信息 //需要重新改动
    public Result detail(@PathVariable(name="id") Long id){
        Tick tick = tickService.getById(id);
        Assert.notNull(tick,"该票务已被删除");
        return  Result.succ(tick);
    }




    @RequiresAuthentication
    @PostMapping("/tick/edit")
    public Result edit(@Validated @RequestBody Tick blog){
        Tick temp=null;
        if(blog.getId()!=null){
            temp=tickService.getById(blog.getId());
            //只能编辑自己的文章
            Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(),"没有权限编辑");
        }else
        {
            temp = new Tick();
            temp.setUserId(ShiroUtil.getProfile().getId().intValue());
            temp.setCreated(LocalDateTime.now());
            //temp.setStatus(1);

        }
       temp.setPicture(blog.getPicture()).setContet(blog.getContet()).setTitle(blog.getTitle()).setDescription(blog.getDescription()).setNum(blog.getNum())
               .setSwiper(blog.getSwiper()).setSwipera(blog.getSwipera()).setStarttime(blog.getStarttime())
               .setSwiperb(blog.getSwiperb()).setPrice(blog.getPrice()).setStatus(blog.getStatus());
        tickService.saveOrUpdate(temp);

        return  Result.succ(null);
    }






}
